package com.beici.waterghost.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beici.waterghost.mapper.userMapper;
import com.beici.waterghost.model.idRandom;
import com.beici.waterghost.model.resultes;
import com.beici.waterghost.model.user;
import com.beici.waterghost.service.impl.loginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 处理qq小程序请求
 * @auther WaterGhost
 * @date 2022/2/28 9:27
 */
@Controller
@RestController
@RequestMapping("/miniapp")
public class qqMiniAppController {

    // 登录
    @Autowired
    private loginService loginServer;
    @Autowired
    private userMapper userService;

    @PostMapping("/login")
    public resultes login(String openID,Long phone){
        user user = userExist(openID, phone);
        resultes login = loginServer.login(String.valueOf(user.getUserPhone()),user.getUserKey());
        return login;
    }

    private user userExist(String openID,Long phone){

        if (phone == null) {
            return userService.selectOne(new QueryWrapper<user>().eq("user_open_id", openID).select("user_phone", "user_key"));
        }
            user u = new user();
            u.setUserName("匿名者-"+ idRandom.getIntRandom());
            u.setUserOpenId(openID);
            u.setUserPhone(phone);
            int insert = userService.insert(new user());
            return userService.selectOne(new QueryWrapper<user>().eq("user_open_id", openID).select("user_phone", "user_key"));
    }



}
